package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.ChannelDetail;
import com.elinkway.infinitemovies.bean.ChannelDetailList;

import org.json.JSONArray;
import org.json.JSONObject;

public class ChannelDetailListParser extends
		LVideoMobileParser<ChannelDetailList> {
	private static final String CHANNELNAME = "channelname";
	private static final String SRC = "src";
	private static final String NAME = "name";
	private static final String RATING = "rating";
	private static final String POSTER = "poster";
	private static final String ISEND = "isend";
	private static final String STARRINGNAME = "starringname";
	private static final String PLAYSIT = "playsit";
	private static final String RELEASEDATE = "releasedate";
	private static final String EPISODES = "episodes";
	private static final String VT = "vt";
	private static final String AREANAME = "areaname";
	private static final String NOWEPISODES = "nowepisodes";
	private static final String AID = "aid";
	private static final String SUBCATEGORYNAME = "subcategoryname";
	

	@Override
	public ChannelDetailList parse(JSONObject data) throws Exception {

		ChannelDetailList channelList = new ChannelDetailList();
		channelList.setName(data.optString(CHANNELNAME));
		JSONArray arr = data.getJSONArray("data");
		for (int i = 0; i < arr.length(); i++) {
			JSONObject obj = arr.getJSONObject(i);
			ChannelDetail channelDetail = new ChannelDetail();
			if (obj.has(SRC)){
			    channelDetail.setSrc(obj.getString(SRC));
			    }
			if (obj.has(NAME)){
			    channelDetail.setName(obj.getString(NAME));
			    }
			if (obj.has(RATING)){
				String score=String.format("%.1f",obj.optDouble(RATING));
			    channelDetail.setRating(score);
			    }
			if (obj.has(POSTER)){
			    channelDetail.setPoster(obj.getString(POSTER));
			    }
			if (obj.has(ISEND)){
			    channelDetail.setIsend(obj.getString(ISEND));
			    }
			if (obj.has(STARRINGNAME)){
			    channelDetail.setStarringname(obj.getString(STARRINGNAME));
			    }
			if (obj.has(PLAYSIT)){
			    channelDetail.setPlaysit(obj.getString(PLAYSIT));
			    }
			if (obj.has(RELEASEDATE)){
			    channelDetail.setReleasedate(obj.getString(RELEASEDATE));
			    }
			if (obj.has(EPISODES)){
			    channelDetail.setEpisodes(obj.getString(EPISODES));
			    }
			if (obj.has(VT)){
			    channelDetail.setVt(obj.getString(VT));
			    }
			if (obj.has(AREANAME)){
			    channelDetail.setAreaname(obj.getString(AREANAME));
			    }
			if (obj.has(NOWEPISODES)){
			    channelDetail.setNowepisodes(obj.getString(NOWEPISODES));
			    }
			if (obj.has(AID)){
			    channelDetail.setAid(obj.getString(AID));
			    }
			if (obj.has(SUBCATEGORYNAME)){
			    channelDetail.setSubcategoryname(obj.getString(SUBCATEGORYNAME));
			    }

			channelList.getChannelDetails().add(channelDetail);
		}
		return channelList;
	}

}